package com.ptbuy.tranrecord.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import com.ptbuy.R;
import com.ptbuy.db.pojo.ExportPojo;
import com.ptbuy.db.pojo.ProductPojo;
import com.ptbuy.utils.MineType;
import com.ptbuy.utils.ToastUtil;

import java.io.File;
import java.util.List;

public class ExportViewAdapter extends ArrayAdapter<ExportPojo> {

    private Context context;

    public interface ExportViewInterface {
        void opt(ExportPojo dataBean);
    }

    public ExportViewInterface exportViewInterface;

    public void setProjectInterface(ExportViewInterface projectInterface) {
        this.exportViewInterface = projectInterface;
    }

    private void opt(ExportPojo dataBean) {
        exportViewInterface.opt(dataBean);
    }

    public ExportViewAdapter(Context context, List<ExportPojo> items) {
        super(context, 0, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.tran_historical_export_item, parent, false);
        }
        ExportPojo item = getItem(position);
        TextView exportPath = convertView.findViewById(R.id.exportPath);
        exportPath.setText(item.getExportFilePath());
        TextView exportDate = convertView.findViewById(R.id.exportDate);
        exportDate.setText(item.getExportDate());

        convertView.findViewById(R.id.exportPreview).setOnClickListener(v -> {
            File file = new File(item.getExportFilePath());
            if (file.exists()) {
                MineType.openFile(context, file);
            } else {
                ToastUtil.show(context, "文件丢失");
            }
        });
        //删除
        convertView.findViewById(R.id.exportRemove).setOnClickListener(v -> {
            opt(item);
        });

        return convertView;
    }
}

